package at.fhj.itm10.ticketmaster.helper;
/**
 * 
 * @author Gerald Hambrosch
 *
 */
import java.sql.SQLException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import at.fhj.itm10.ticketmaster.beans.EventBean;
import at.fhj.itm10.ticketmaster.beans.EventTypeBean;
import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.models.EventType;

public class CategoryConverter implements Converter {

	private EventType category;
	private EventBean b;
	private EventTypeBean c;

	public Object getAsObject(FacesContext context, UIComponent component, String value)
			throws ConverterException {
		
		if (value == null || value.equals("")) {
            return "";
        } 
		else 
		{
				c = new EventTypeBean();
				b = new EventBean();
                long id = Long.parseLong((value));
						try {
							category = c.getDao().findById(id);
						} catch (DAOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						try {
							b.setCategory(category);
							b.doExtendedSearch();
						} catch (DAOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				
                    return category;    
        }

	}


	public String getAsString(FacesContext context, UIComponent component, Object value)
			throws ConverterException {
	       if (value == null || value.equals("")) {
	            return "";
	        } else {
	            return String.valueOf(((EventType) value).getId());
	        }

	}

}
